package com.jxwifi.cloud.quickcleanserver.order;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicActivity;
import com.idroid.widget.LoadDialog;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.CleanBasicActivity;
import com.jxwifi.cloud.quickcleanserver.app.StateUtils;
import com.jxwifi.cloud.quickcleanserver.app.UrlPool;
import com.jxwifi.cloud.quickcleanserver.bean.CommentBean;
import com.jxwifi.cloud.quickcleanserver.bean.DetailsBean;
import com.jxwifi.cloud.quickcleanserver.bean.OrderFromBean;
import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;
import com.jxwifi.cloud.quickcleanserver.utils.PasswordLogin;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;
import com.jxwifi.cloud.quickcleanserver.utils.TokenTimeUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/5/17.
 */

public class OrderDetailsActivity extends CleanBasicActivity {

    @Bind(R.id.tv_order_details_state)
    TextView mTvOrderDetailsState;
    @Bind(R.id.tv_order_details_settlement)
    TextView mTvOrderDetailsSettlement;
    @Bind(R.id.tv_map_work_order_type_text)
    TextView mTvMapWorkOrderTypeText;
    @Bind(R.id.tv_form_order_service_time_text)
    TextView mTvFromOrderServiceTimeText;
    @Bind(R.id.tv_map_work_order_address_text)
    TextView mTvMapWorkOrderAddressText;
    @Bind(R.id.tv_map_work_measure_text)
    TextView mTvMapWorkMeasureText;
    @Bind(R.id.tv_map_work_cost_text)
    TextView mTvMapWorkCostText;
    @Bind(R.id.tv_map_work_remarks_text)
    TextView mTvMapWorkRemarksText;
    @Bind(R.id.tv_include_login_fraction)
    TextView mTvIncludeLoginFraction;
    @Bind(R.id.tv_map_work_service_type_text)
    TextView mTvMapWorkServiceTypeText;
    @Bind(R.id.lin_map_work_remarks)
    LinearLayout mLinMapWorkRemarks;
    @Bind(R.id.lin_order_details_comment)
    LinearLayout mLinOrderDetailsConmment;
    @Bind(R.id.rb_shop_details_list)
    RatingBar mRbShopDetailsList;
    @Bind(R.id.sv_order_scroll)
    ScrollView mSvOrderScroll;
    private LoadDialog mLoadDialog;//加载动画
    private String mOrderCode;
    private OrderFromBean morderFromBean;
    private CommentBean mCommentBean;//评价信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        ButterKnife.bind(this);
        openEventBus();
        mOrderCode=getIntent().getStringExtra("order_code");
        mLoadDialog = new LoadDialog(mBaseContext, "");
        mLoadDialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                initdata();
                mLoadDialog.dismiss();
            }
        }, 1000);
    }

    @OnClick({R.id.rel_img_back_pressed})
    void clickCK(View v){
        switch (v.getId()){
            case R.id.rel_img_back_pressed:
                onBackPressed();
                break;
        }
    }
    public void initView(){
        mSvOrderScroll.setVisibility(View.VISIBLE);
        if (morderFromBean.getOrderStatus().equals(StateUtils.SER_END)){
            mTvOrderDetailsState.setText("订单已结算");
            mTvMapWorkOrderTypeText.setText("已完成");
        }else{
            mTvOrderDetailsState.setText("订单未付款");
            mTvMapWorkOrderTypeText.setText("未付款");
        }
        mTvMapWorkOrderTypeText.setText(morderFromBean.getOrderCode());
        mTvMapWorkServiceTypeText.setText(morderFromBean.getProductName());
        mTvFromOrderServiceTimeText.setText(morderFromBean.getServiceTime());
        if (morderFromBean.getProvince()==null||morderFromBean.getCity()==null||morderFromBean.getArea()==null){
            mTvMapWorkOrderAddressText.setText(morderFromBean.getAddress());
        }else{
            mTvMapWorkOrderAddressText.setText(morderFromBean.getProvince()+""+morderFromBean.getCity()+""+morderFromBean.getArea()+""+morderFromBean.getAddress());
        }

        mTvMapWorkMeasureText.setText(TokenTimeUtils.changeurs((int)morderFromBean.getServiceTimeSecond()));//服务时长
        mTvMapWorkCostText.setText(morderFromBean.getTotalAmount()+"元");
        mTvOrderDetailsSettlement.setText("结算时间："+morderFromBean.getServiceEndTime());
        if (!TextUtils.isEmpty(morderFromBean.getRemark())){
            mTvMapWorkRemarksText.setText(morderFromBean.getRemark());
        }else{
            mLinMapWorkRemarks.setVisibility(View.GONE);
        }
        if (mCommentBean==null){
            mLinOrderDetailsConmment.setVisibility(View.GONE);
        }else{
            if (mCommentBean.getGrade()!=0){
                mLinOrderDetailsConmment.setVisibility(View.VISIBLE);
                mTvIncludeLoginFraction.setText(mCommentBean.getGrade()+"分");
                mRbShopDetailsList.setRating((float) (mCommentBean.getGrade() / 20));
            }else{
                mLinOrderDetailsConmment.setVisibility(View.GONE);
            }
        }
    }

    public void initdata(){
        Params params=new Params();
        params.add("orderCode",mOrderCode);
        AuzHttp.get(UrlPool.GET_ORDER_INFO, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                if (code==0){
                    DetailsBean mdetailsBean= JSON.parseObject(data, DetailsBean.class);
                    morderFromBean = mdetailsBean.getOrderInfo();
                    mCommentBean=mdetailsBean.getComment();
                    initView();
                }else{
                    PasswordLogin.passwordlogin(mBaseContext,TAG, AccountMgr.gettell(mBaseContext),AccountMgr.getpassword(mBaseContext));
                    if (PasswordLogin.mIsLogin){
                        initdata();
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast =Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast,3000);
            }
        },TAG);
    }
}
